export const debounce = (callback: Function, time: number): Function => {
    let timer: any
    return (...args: any) => {
        if (timer) clearTimeout(timer)
        timer = setTimeout(() => {
            callback.apply(null , args)
            timer = undefined
        }, time)
    }
}

export const clearObjectFields = (obj: any) => {
    const keys = Object.keys(obj)
    for (let key of keys) {
        obj[key] = void 0
    }
}

export const validatePhone = (rule: any, value: any, callback: any) => {
    if (!value || !value.length) {
        callback(new Error('请输入手机号码！'))
    } else if (!/^1([0-9]{10})$/.test(value)) {
        callback(new Error("请输入正确的手机号码!"))
    } else {
        callback()
    }
}

export const splitAddress = (address: string): string[] | null => {
    if (!address) return null
    let regExp = /.+?(省|市|自治区|自治州|县|区)/g
    return address.match(regExp)
}
